/*
 * @Author: FengKongLi 
 * @Date: 2021-08-23 18:56:31 
 * @Last Modified by: FengKongLi
 * @Last Modified time: 2021-08-24 20:21:34
 */
// 轮播管理页面
<template>
  <div>
    <div>
      <el-button @click="toAdd" class="btn" type="primary">新增</el-button>
      <el-button @click="toPreview" class="btn" type="primary">预览</el-button>
    </div>
    <!-- 表格数据展示 -->
    <div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column prop="name" label="轮播图名称" align="center">
        </el-table-column>
        <el-table-column prop="introduce" label="轮播图描述"> </el-table-column>
        <el-table-column label="轮播图图片" width="230" align="center">
          <template slot-scope="scope">
            <el-image
              style="width: 200px; height: 100px"
              :src="scope.row.url"
              :preview-src-list="[scope.row.url]"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === '正常' ? 'success' : 'danger'">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button @click="toEdit(scope.row)" type="text">编辑</el-button>
            <el-button @click="toDelete(scope.row.id)" type="text"
              >删除</el-button
            >
            <el-button
              v-if="scope.row.status === '正常'"
              @click="toStopAndUse(scope.row)"
              type="text"
              >停用</el-button
            >
            <el-button v-else @click="toStopAndUse(scope.row)" type="text"
              >启用</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增、编辑对话框 -->
    <div>
      <el-dialog :title="title" :visible.sync="visible" @close="toClose">
        <el-form :model="form" :rules="rules" ref="ruleForm">
          <el-form-item prop="name" label="名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="url" label="封面" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              :action="uploadFileURL"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item
            prop="description"
            v-model="form.introduce"
            label="描述"
            :label-width="formLabelWidth"
          >
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form.introduce"
            >
            </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 大屏 预览对话框-->
    <div>
      <el-dialog title="大屏预览" :visible.sync="BigScreenVisible" width="80%">
        <!-- 轮播图开始 -->
        <div>
          <el-carousel height="400px">
            <el-carousel-item v-for="item in urlArr" :key="item.id">
              <img :src="item.url" style="width: 100%; height: 400px" />
            </el-carousel-item>
          </el-carousel>
        </div>
        <!-- 轮播图结束 -->
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="BigScreenVisible = false"
            >关闭</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { findAll, saveOrUpdate, query, deleteById } from "@/api/carousel";
import { showFileURL, uploadFileURL } from "@/utils/config";
export default {
  data() {
    return {
      showFileURL,
      uploadFileURL,
      tableData: {},
      form: {},
      formLabelWidth: "80px",
      title: "",
      visible: false,
      //图片url
      imageUrl: "",
      //   大屏预览轮播图数据
      urlArr: [],
      // 大屏预览轮播图数据请求时传的状态
      status: "",
      rules: {
        name: [
          { required: true, message: "请输入轮播图名称", trigger: "blur" },
        ],
        url: [{ required: true, message: "请上传轮播图", trigger: "change" }],
      },
      //   大屏预览对话框显示与隐藏
      BigScreenVisible: false,
    };
  },
  created() {
    this.findAllData();
  },
  methods: {
    //   查询所有轮播图数据
    async findAllData() {
      let res = await findAll();
      this.tableData = res.data;
      //console.log(res.data);
    },
    //添加按钮事件处理程序
    toAdd() {
      this.findAllData();
      // 在对话框再次打开时清空表单数据
      this.form = {};
      this.imageUrl = "";
      this.title = "新增轮播图";
      this.visible = true;
    },
    //图片上传成功的回调函数
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      // this.form.url = showFileURL + res.data.id;
      // 从图片服务器请求图片路径存到表单
      this.form.url = file.response;
    },
    //图片上传之前的回调函数
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //对话框确定按钮事件处理程序
    toSave() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          // 校验通过，做保存
          // console.log("校验通过");
          let res = await saveOrUpdate(this.form);
          // 提示用户保存成功，刷新表格数据，关闭对话框
          this.$notify.success({
            title: "保存成功",
            message: res.message,
          });
          //刷新数据就是重新请求数据,调用请求方法
          this.findAllData();
          this.visible = false;
        } else {
          // 校验不通过
          return false;
        }
      });
    },
    toClose() {
      // 重置表单验证
      this.$refs.ruleForm.resetFields();
    },
    // 编辑按钮
    toEdit(row) {
      this.findAllData();
      // 设置表单数据
      this.form = { ...row };
      this.imageUrl = this.form.url;
      this.title = "编辑轮播图";
      this.visible = true;
    },
    // 删除按钮
    async toDelete(id) {
      this.$confirm("此操作将永久删除这条数据,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await deleteById({ id });
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "删除成功",
            });
          }
          this.findAllData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 大屏预览轮播图数据查询
    async toPreview() {
      this.BigScreenVisible = true;
      let res = await query({ status: "正常" });
      console.log(res.data);
      this.urlArr = res.data;
    },
    // 点击停用按钮
    async toStopAndUse(row) {
      if (row.status === "正常") {
        row.status = "停用";
        let res = await saveOrUpdate({ ...row });
        if (res.status === 200) {
          this.$message({
            type: "success",
            message: "停用成功",
          });        
        } else {
          this.$message({
            type: "danger",
            message: "停用失败",
          });        
        }
      }
      else{
         row.status = "正常";
         let res = await saveOrUpdate({ ...row });
        if (res.status === 200) {
          this.$message({
            type: "success",
            message: "启用成功",
          });        
        } else {
          this.$message({
            type: "danger",
            message: "启用失败",
          });        
        }
      }
      this.findAllData();
    },
  },
};
</script>


<style scoped>
</style>